﻿#region Copyright © 2008 by Christian Rodemeyer
// $HeadURL: http://rodemeyer.googlecode.com/svn/trunk/Rodemeyer/SafeConvert.cs $
// $Revision: 27 $
#endregion

using System;
using System.Collections.Generic;
using System.Text;

namespace Rodemeyer
{
    /// <summary>
    /// The same as System.Convert, but instead of throwing exceptions it returns the default value
    /// </summary>
    public static class SafeConvert
    {
        public static int ToInt(string s)
        {
            int i;
            int.TryParse(s, out i);
            return i;
        }

        public static Double ToDouble(string s)
        {
            double d;
            Double.TryParse(s, out d);
            return d;
        }

        public static bool ToBool(string s)
        {
            bool b;
            Boolean.TryParse(s, out b);
            return b;
        }

        public static DateTime ToDateTime(string s)
        {
            DateTime dt;
            DateTime.TryParse(s, out dt);
            return dt;
        }

        public static TimeSpan ToTimeSpan(string s)
        {
            TimeSpan ts;
            TimeSpan.TryParse(s, out ts);
            return ts;
        }

    }
}
